﻿using System;
using System.ComponentModel.Composition;
using Microsoft.Office.Interop.Word;

namespace Songhay.Office2010.WordWalkingStick.Commands
{
    using Songhay.Models;
    using Songhay.Office2010.Word;

    [Export(typeof(IOfficeWordCommand))]
    public class CollapseParagraphsToLineBreaksCommand : IOfficeWordCommand
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="CollapseParagraphsToLineBreaksCommand"/> class.
        /// </summary>
        public CollapseParagraphsToLineBreaksCommand()
        {
            this.Name = "CollapseParagraphsToLineBreaksCommand";
        }

        #region IOfficeWordCommand Members

        /// <summary>
        /// Gets or sets the name
        /// (non-unique key)
        /// for locating this command
        /// in a collection of commands.
        /// </summary>
        /// <value>The name.</value>
        /// <remarks>
        /// Conventional command name prefixes:
        /// Startup* Commands with this prefix will be executed during the ThisAddIn_Startup event.
        /// Shutdown* Commands with this prefix will be executed during the ThisAddIn_Shutdown event.
        /// </remarks>
        public string Name { get; set; }

        /// <summary>
        /// Executes the command.
        /// </summary>
        /// <typeparam name="TMessage">The type of the message.</typeparam>
        /// <param name="wordApplication">The word application.</param>
        /// <param name="message">The message.</param>
        public void ExecuteCommand<TMessage>(Application wordApplication, TMessage message) where TMessage : IControlInteractionMessage
        {
            var range = wordApplication.Selection.Range;

            if(range.Paragraphs.Count == 0) return; //TODO: consider throwing exceptions to be caught by Ribbon/WPF.

            range.Text = range.Text
                .Replace(WordCharacters.Cr, WordCharacters.LineBreak);
        }

        /// <summary>
        /// Executes the command for binding.
        /// </summary>
        /// <typeparam name="TMessage">The type of the message.</typeparam>
        /// <param name="wordApplication">The word application.</param>
        /// <param name="message">The message.</param>
        /// <returns></returns>
        public object ExecuteCommandForBinding<TMessage>(Application wordApplication, TMessage message) where TMessage : IControlInteractionMessage
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
